<!-- productbatchmodel -->
<template>
  <a-card
    :bordered="false"
    :body-style="{ 'padding-bottom': '0px' }"
    :style="{ margin: '6px', paddingBottom: '0px' }"
  >
    <a-form
      ref="searchFormRef"
      name="advanced_search"
      :model="searchFormState"
      class="ant-advanced-search-form"
    >
      <a-row :gutter="24">
        <a-col :span="4">
          <a-form-item label="产品型号" name="PMID">
            <a-select
              v-model:value="searchFormState.PMID"
              style="width: 100%"
              placeholder="请选择产品型号"
              
              :options="productModel"
            ></a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-button class="mr10" type="primary" @click="tableRef.refresh(true)"
            >查询</a-button
          >
          <a-button class="xn-mg08" @click="reset">重置</a-button>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <a-card :bordered="false" :style="{ margin: '6px' }">
    <m-table
      :show-pagination="true"
      ref="tableRef"
      :columns="columns"
      :data="loadData"
      :alert="options.alert.show"
      bordered
      :row-key="(record) => record.ID"
      :tool-config="toolConfig"
      :row-selection="options.rowSelection"
      :scroll="{ x: 'max-content' }"
    >
      <template #operator>
        <a-space>
          <a-button type="primary" @click="formRef.onOpen()">
            <template #icon><plus-outlined /></template>
            新增
          </a-button>
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'PTID'">
          {{
            productType.filter((item) => item.value === record.PTID)[0].label
          }}
        </template>
        <template v-if="column.dataIndex === 'UpdateTime'">
          {{ transitionTime(record.UpdateTime) }}
        </template>
        <template v-if="column.dataIndex === 'CreateTime'">
          {{ transitionTime(record.CreateTime) }}
        </template>
        <template v-if="column.dataIndex === 'PBBeginTime'">
          {{ transitionTime(record.PBBeginTime) }}
        </template>
        <template v-if="column.dataIndex === 'PBEndTime'">
          {{ transitionTime(record.PBEndTime) }}
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button type="link" @click="formRef.onOpen(record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-popconfirm
            title="确定删除？"
            @confirm="deleteModule(record)"
            ok-text="确定"
            cancel-text="关闭"
          >
            <a-button type="link" danger size="small">删除</a-button>
          </a-popconfirm>
        </template>
      </template>
    </m-table>
  </a-card>
  <Form
    ref="formRef"
    @successful="tableRef.refresh(true)"
    :id="route.query.id"
  />
</template>
      
  <script setup name="productbatchmgr">
import productbatchmgrApi from "@/api/productManagement/productbatchmgrApi";
import usermgrApi from "@/api/systemManagement/usermgrApi";
import productmodelmgrApi from "@/api/productManagement/productmodelmgrApi";
import producttypemgrApi from "@/api/productManagement/producttypemgrApi";

import Form from "./form.vue";
import { cloneDeep } from "lodash-es";
import { message } from "ant-design-vue";
import {
  transitionTime,
  getPreviousMonthDateTime,
  getCurrentDateTime,
} from "@/utils/time.js";
import { ref, watch } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();

watch(
  () => route.query.id,
  (n, o) => {
    tableRef.value.refresh();
  }
);
const searchFormState = ref({
  PMID: 0,
});
const formRef = ref();
const searchFormRef = ref();
const tableRef = ref();
const toolConfig = {
  refresh: true,
  height: true,
  columnSetting: false,
  striped: false,
};

const columns = [
  {
    title: "批次名称",
    dataIndex: "PBName",
    align: "center",
  },
  {
    title: "订单编号",
    dataIndex: "PBNo",
    align: "center",
  },
  {
    title: "型号名称",
    dataIndex: "PMName",
    align: "center",
  },
  {
    title: "型号编号",
    dataIndex: "PMNo",
    align: "center",
  },
  {
    title: "产品类型",
    dataIndex: "PTID",
    align: "center",
  },

  {
    title: "产产品起始编码",
    dataIndex: "StartSN",
    align: "center",
  },
  {
    title: "产产品结束编码",
    dataIndex: "EndSN",
    align: "center",
  },
  /* {
    title: "产品数量",
    dataIndex: "NumPerTuoPan",
    align: "center",
  }, */
  /*  {
    title: "计划中已完成老化的产品数量",
    dataIndex: "AgeFinishCnt",
    align: "center",
  },
  {
    title: "计划中检测合格的产品数量",
    dataIndex: "CheckPassCnt",
    align: "center",
  },
  {
    title: "计划中已打包的产品数量",
    dataIndex: "HasPackageCnt",
    align: "center",
  }, */
  {
    title: "产品数量",
    dataIndex: "Num",
    align: "center",
  },
  {
    title: "开始时间",
    dataIndex: "PBBeginTime",
    align: "center",
  },
  {
    title: "结束时间",
    dataIndex: "PBEndTime",
    align: "center",
  },
  {
    title: "数量/托盘",
    dataIndex: "NumPerTuoPan",
    align: "center",
  },

  {
    title: "创建时间",
    dataIndex: "CreateTime",
    align: "center",
  },
  {
    title: "更新时间",
    dataIndex: "UpdateTime",
    align: "center",
  },
  {
    title: "更新人",
    dataIndex: "CreateUserName",
    align: "center",
  },
  /* 
  {
    title: "批次说明",
    dataIndex: "PBDesp",
    align: "center",
  }, */
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    width: "280px",
    fixed: "right",
  },
];

// 列表选择配置
const options = {
  alert: {
    show: false,
    clear: () => {},
  },
};

const loadData = (parameter) => {
  fetchProductType();
  fetchProductModel();
  const searchFormParam = cloneDeep(searchFormState.value);
  // 把查询条件保存到导出条件中
  return productbatchmgrApi
    .getProductBatchModels(
      Object.assign(parameter, searchFormParam, {
        PBID: route.query.id,
      })
    )
    .then((res) => {
      return res;
    });
};

// 重置
const reset = () => {
  searchFormRef.value.resetFields();
  tableRef.value.refresh(true);
};

// 产品型号下拉框数据

let productModel = ref([]);
const fetchProductModel = () => {
  productmodelmgrApi
    .getProductModels({
      ID: 0,
      PMName: "",
      PMNo: "",
      PTID: 0,
    })
    .then((res) => {
      productModel.value = res.Data.map((i) => {
        return {
          label: i.PMName,
          value: i.ID,
        };
      });
      productModel.value.unshift({
        label: "全部",
        value: 0,
      });
    });
};

// 产品类型
let productType = ref([]);
const fetchProductType = () => {
  producttypemgrApi
    .getProductTypes({
      ID: 0,
      PTName: "",
      PTNo: "",
    })
    .then((res) => {
      productType.value = res.Data.map((i) => {
        return {
          label: i.ProductTypeName,
          value: i.ID,
         
        };
      });
    });
};
// 删除
const deleteModule = (record) => {
  productbatchmgrApi
    .deleteProductBatchModel({
      ID: record.ID,
    })
    .then((res) => {
      if (!res.Result) {
        message.error("删除失败");
        return;
      }
      message.success("删除成功");
      tableRef.value.refresh();
    })
    .catch(() => {
      message.error("删除失败");
    });
};
onMounted(() => {
  console.log("111");
});
</script>
      